/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.chatapp;

import java.util.ArrayList;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Group C
 */
public class ImpListener implements Listener, PacketListener
{
    String search =null;  
    boolean encontrar=false;
    String respuesta= null;
    //Define the Logger for ImpListener 
    private final static Logger log = Logger.getLogger(ImpListener.class);
    
    
 public ImpListener(){}

 public void processPacket(Packet packet)
 {
     //Basic Configuration for log4j
     BasicConfigurator.configure();
      
     if (packet instanceof Message) 
      {    
          Message msg = (Message) packet;
             
          if( (GenericMessage) msg.getProperty("Type") instanceof SearchProfile)
          {
             searchProfile(msg);
          }     
          else if( (GenericMessage) msg.getProperty("Type") instanceof SearchResult)
          {
             searchResult(msg); 
          }
        }               
    }
 
    public void searchProfile (Message msg)
    {
       
        ImpSearchProfile content = (ImpSearchProfile) msg.getProperty("Type");
        search = content.getContent();
        
        //print the content of SearchProfile
        log.info("El contendido buscado es: "+search);

        encontrar =  ImpInstantMessage.getIntanceMessage().searchContent(search);  

        if(encontrar)
        {     
            ImpSearchResult result = new ImpSearchResult(ImpInstantMessage.getIntanceMessage().getContent(search),msg.getFrom());
            ImpInstantMessage.getIntanceMessage().sendMessageSearchResult(result,msg.getFrom());                
        }
         
    }
    
    public void searchResult (Message msg)
    {
        ImpSearchResult content = (ImpSearchResult) msg.getProperty("Type");
            
        ArrayList <ImpContent> result =content.getContent();
        
        //show the value of the Searched Content
        log.info("lo que se ha encontrado es: "+result.get(0).getNamePhoto());
    }
}    